package com.itheima.reggie.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @Description:test
 * @Create by: CAt
 * @Date;2021/9/2315:13
 */

@RestControllerAdvice(annotations = {Controller.class, RestController.class})
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> exceptionhandler(SQLIntegrityConstraintViolationException sq) {
        log.error(sq.getMessage());
        if (sq.getMessage().contains("Duplicate entry")) {
            String[] s = sq.getMessage().split(" ");
            return R.error(s[2] + "已被占用");
        }
        return R.error("系统繁忙");
    }
    @ExceptionHandler(CustomException.class)
    public R<String> customException(CustomException us) {
        log.error(us.getMessage());

        return R.error(us.getMessage());
    }
}
